﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ChangeProblem.Logic
{
    public class OddChange
    {
        //钱币的面           
        decimal[] value = { 100, 50, 20, 10, 5, 1, 0.5M, 0.1M };

        public int[] Change(decimal change, int[] moneyAmountInBox)
        {

            //钱的张数
            int[] moneyAmount = new int[value.Length];

           //计算找零对应面值的张数
            for (int i = 0; i < value.Length; i++)
            {
                moneyAmount[i] = (int)(change / value[i]);
                if (moneyAmount[i] > moneyAmountInBox[i])
                {
                    change -= value[i] * moneyAmountInBox[i];
                    moneyAmount[i] = moneyAmountInBox[i];
                }
                else
                {
                    change = change % value[i];
                }
            }
            return moneyAmount;
        }

        public decimal Rest(decimal moneyOfPay, decimal pricesForGoods)
        {
            decimal change = moneyOfPay - pricesForGoods;
            return change;
        }

        /// <summary>
        /// 判断是否找零成功
        /// </summary>
        /// <param name="change"></param>
        /// <param name="moneyAmount"></param>
        public int[] IsSuccess(decimal change, int[] moneyAmount)
        {
            decimal sum = 0;
            for (int i = 0; i < value.Length; i++)
            {
                sum += value[i] * moneyAmount[i];
            }
            if (sum != change)
            {
                moneyAmount = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
                return moneyAmount;
            }

            return moneyAmount;
        }

        #region 计算付钱后钱柜中各面值钱币的张数      
        public int[] Deposit(int[] moneyAmountInBox, int[] pay)
        {
            for (int i = 0; i < pay.Length; i++)
            {
                moneyAmountInBox[i] += pay[i];
            }

            return moneyAmountInBox;
        }
        #endregion


        ///计算找零后钱柜的状态
        public int[] moneyAmountInBoxAfter(int[] moneyAmountInBox, int[] ChangeAmount)
        {
            int[] moneyAmountInBoxAfter = new int[8];
            for (int i = 0; i < value.Length; i++)
            {
                moneyAmountInBoxAfter[i] = moneyAmountInBox[i] - ChangeAmount[i];
            }
            return moneyAmountInBoxAfter;
        }

    }
}